﻿namespace Ex03.GarageLogic
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public enum eLicenseTypes
    {
        A1,
        A2,
        A3,
        B
    }

    internal class Motorcycle : Vehicle
    {
        #region Messages
        private const string k_StringNotDefinedInLicenseTypesEnum = "{0} is not an allowed license type.";
        private const string k_StringEngineCapacityToIntError = "{0} is not an allowed engine capacity.";
        private const string k_Cc = "{0} cc";
        private const string k_LicenseType = "License type (A1, A2, A3 or B): ";
        private const string k_EngineCapacity = "Engine capacity (in cc): ";
        private const string k_DisplayLicenseType = "License type: ";
        private const string k_DisplayEngineCapacity = "Engine capacity (in cc): ";
        #endregion

        #region Data Members
        private eLicenseTypes m_LicenseType;
        private int m_EngineCapacity;
        #endregion

        #region Constructors
        public Motorcycle(string i_LicenseNumber, Engine i_Engine, int i_NumOfWheels, float i_MaxPSI)
            : base(i_LicenseNumber, i_Engine, i_NumOfWheels, i_MaxPSI)
        {
        }
        #endregion

        #region Properties
        public string LicenseType
        {
            get { return m_LicenseType.ToString(); }
            set
            {
                try
                {
                    eLicenseTypes newLicenseType = (eLicenseTypes)Enum.Parse(typeof(eLicenseTypes), value.ToUpper());
                    if (!Enum.IsDefined(typeof(eLicenseTypes), newLicenseType))
                    {
                        throw new FormatException(string.Format(k_StringNotDefinedInLicenseTypesEnum, value));
                    }
                    else
                    {
                        m_LicenseType = newLicenseType;
                    }
                }
                catch (Exception ex)
                {
                    throw new FormatException(string.Format(k_StringNotDefinedInLicenseTypesEnum, value), ex);
                }
            }
        }

        public string EngineCapacity 
        {
            get { return string.Format(k_Cc, m_EngineCapacity.ToString()); }
            set
            {
                int intEngineCapacity = 0;
                bool goodInput = false;
                goodInput = int.TryParse(value, out intEngineCapacity);

                if (goodInput == false)
                {
                    throw new FormatException(string.Format(k_StringEngineCapacityToIntError, value));
                }
                else
                {
                    m_EngineCapacity = intEngineCapacity;
                }
            }
        }
        #endregion

        #region Public Methods
        public override void GetAllDetails(ref List<string> io_ListOfVehicleDetails)
        {
            base.GetAllDetails(ref io_ListOfVehicleDetails);
            io_ListOfVehicleDetails.Add(string.Format(GarageLogic.s_PrintParameter, k_DisplayLicenseType, LicenseType));
            io_ListOfVehicleDetails.Add(string.Format(GarageLogic.s_PrintParameter, k_DisplayEngineCapacity, EngineCapacity));
            base.GetEngineAndWheelsDetails(ref io_ListOfVehicleDetails);
        }

        public override void GetClassParametersNeededToCreate(ref List<string> io_ListOfParameters)
        {
            base.GetClassParametersNeededToCreate(ref io_ListOfParameters);
            io_ListOfParameters.Add(k_LicenseType);
            io_ListOfParameters.Add(k_EngineCapacity);
        }

        public override void SetClassByParameters(ref Dictionary<string, string> io_Parameters)
        {
            base.SetClassByParameters(ref io_Parameters);

            if (GarageLogic.KeyFoundInDictionaryAndNotEmptyOrNull(io_Parameters, k_LicenseType))
            {
                LicenseType = io_Parameters[k_LicenseType];
                io_Parameters[k_LicenseType] = string.Empty;
            }

            if (GarageLogic.KeyFoundInDictionaryAndNotEmptyOrNull(io_Parameters, k_EngineCapacity))
            {
                EngineCapacity = io_Parameters[k_EngineCapacity];
                io_Parameters[k_EngineCapacity] = string.Empty;
            }
        }
        #endregion
    }
}
